package com.jwgf.sdcard;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import android.os.Bundle;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;

import com.jwgf.sdcard.StorageInterface.SDMsgRecvListener;

public class SDCardManager {
	private static SDCardManager sdCardManager = new SDCardManager();
	private static Messenger send;
	private static Messenger recv;
	private SDMsgRecvListener listener;

	public static SDCardManager getInstance() {
		if (send == null || recv == null) {
			return null;
		} else {
			return sdCardManager;
		}
	}

	public static SDCardManager create(Messenger sendMsger, Messenger recvMsger) {
		send = sendMsger;
		recv = recvMsger;
		return sdCardManager;
	}

	public void registerSDMsgRecvListener(SDMsgRecvListener listener) {
		this.listener = listener;
	}

	public void process(Message msg) {
		if (listener != null) {
			listener.OnSDMsgRecv(msg.what, msg.arg1, msg.getData());
		}
	}

	public void checkDevice(int isUSB) {
		Message msg;
		msg = Message.obtain();
		msg.replyTo = recv;
		if (isUSB == StorageInterface.Dev.USB) {
			msg.what = StorageInterface.Dev.USB;
		} else {
			msg.what = StorageInterface.Dev.SD;
		}
		msg.arg1 = StorageInterface.Operation.CHECK_DEV;
		try {
			send.send(msg);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	public String getRootPath(Bundle bd) {
		String rootPath = bd.getString(StorageInterface.KEY);
		return rootPath;
	}

	public String findVersionFile(ArrayList<String> fileList) {
		for (String path : fileList) {
			if (path.contains(StorageInterface.VERSION_FILE))
				return path;
		}
		return null;
	}
	
	public String findVersionFile(File[] files) {
		for (File file : files) {
			if (file.getAbsolutePath().contains(StorageInterface.VERSION_FILE))
				return file.getAbsolutePath();
		}
		return null;
	}

	public String getVersion(String path) {
		File fileVersion = null;
		String version = null;

		try {
			fileVersion = new File(path);
			FileInputStream fis = new FileInputStream(fileVersion);
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					fis));
			version = reader.readLine();
			fis.close();
			reader.close();
			return version;
		} catch (IOException e) {
			System.err.println("FileStreamsReadnWrite: " + e);
		}
		return version;
	}

	public boolean checkUpgradeFiles(ArrayList<String> fileList) {
		ArrayList<String> nameList = new ArrayList<String>();
		for (String path : fileList) {
			nameList.add(new File(path).getName());
		}
		return nameList.containsAll(StorageInterface.upgradeFileSet);
	}
	
	public boolean checkUpgradeFiles(File[] fileList) {
		if (fileList == null) {
			return false;
		}
		ArrayList<String> nameList = new ArrayList<String>();
		for (File file : fileList) {
			nameList.add(file.getName());
		}
		return nameList.containsAll(StorageInterface.upgradeFileSet);
	}

	public void upgrade() {
		Message msg;
		msg = Message.obtain();
		msg.replyTo = recv;
		msg.arg1 = StorageInterface.Operation.UPGRADE;
		try {
			send.send(msg);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	public ArrayList<String> getFileList(Bundle bd) {
		return bd.getStringArrayList(StorageInterface.KEY);
	}

	public void dumpLog(int isUSB) {
		Message msg;
		msg = Message.obtain();
		msg.replyTo = recv;
		if (isUSB == StorageInterface.Dev.USB) {
			msg.what = StorageInterface.Dev.USB;
		} else {
			msg.what = StorageInterface.Dev.SD;
		}
		msg.arg1 = StorageInterface.Operation.DUMP_LOG;
		try {
			send.send(msg);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	public void reboot() {
		Message msg;
		msg = Message.obtain();
		msg.replyTo = recv;
		msg.what = StorageInterface.Dev.USB;
		msg.arg1 = StorageInterface.Operation.REBOOT;
		try {
			send.send(msg);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}
}
